﻿package baidu.ui.controls
{
    import baidu.ui.constants.*;
    import baidu.ui.core.*;
    import baidu.ui.events.*;
    import flash.events.*;

    public class ScrollBar extends BUI
    {
        private var _pageSize:Number = 10;
        private var _pageScrollSize:Number = 0;
        private var _lineScrollSize:Number = 1;
        private var _minScrollPosition:Number = 0;
        private var _maxScrollPosition:Number = 0;
        private var _scrollPosition:Number = 0;
        private var _orientation:String = "vertical";
        private var thumbScrollOffset:Number;
        protected var isDragging:Boolean = false;
        protected var up:Button;
        protected var down:Button;
        protected var thumb:LabelButton;
        protected var track:Button;
        public static var defaultStyles:Object = {skin:"ScrollBar_Skin"};
        public static var WIDTH:int = 15;
        public static const THUMB_MIN_HEIGHT:int = 33;

        public function ScrollBar()
        {
            return;
        }// end function

        override public function setSize(param1:Number, param2:Number, param3:Boolean = true) : void
        {
            if (this._orientation == BUIConstants.HORIZONTAL)
            {
                super.setSize(param2, param1, param3);
            }
            else
            {
                super.setSize(param1, param2, param3);
            }
            return;
        }// end function

        override public function get width() : Number
        {
            return this._orientation == BUIConstants.HORIZONTAL ? (super.height) : (super.width);
        }// end function

        override public function get height() : Number
        {
            return this._orientation == BUIConstants.HORIZONTAL ? (super.width) : (super.height);
        }// end function

        override public function set enabled(param1:Boolean) : void
        {
            super.enabled = param1;
            var _loc_2:* = enabled && this._maxScrollPosition > this._minScrollPosition;
            this.up.enabled = enabled && this._maxScrollPosition > this._minScrollPosition;
            var _loc_2:* = _loc_2;
            this.thumb.enabled = _loc_2;
            var _loc_2:* = _loc_2;
            this.track.enabled = _loc_2;
            this.down.enabled = _loc_2;
            this.updateThumb();
            return;
        }// end function

        override public function get classStyles() : Object
        {
            return mergeStyles(BUI.defaultStyles, defaultStyles);
        }// end function

        public function setScrollProperties(param1:Number, param2:Number, param3:Number, param4:Number = 0) : void
        {
            this.pageSize = param1;
            this._minScrollPosition = param2;
            this._maxScrollPosition = param3;
            if (param4 >= 0)
            {
                this._pageScrollSize = param4;
            }
            this.enabled = this._maxScrollPosition > this._minScrollPosition;
            this.setScrollPosition(this._scrollPosition, false);
            this.updateThumb();
            return;
        }// end function

        public function get scrollPosition() : Number
        {
            return this._scrollPosition;
        }// end function

        public function set scrollPosition(param1:Number) : void
        {
            this.setScrollPosition(param1, true);
            return;
        }// end function

        public function get minScrollPosition() : Number
        {
            return this._minScrollPosition;
        }// end function

        public function set minScrollPosition(param1:Number) : void
        {
            this.setScrollProperties(this._pageSize, param1, this._maxScrollPosition);
            return;
        }// end function

        public function get maxScrollPosition() : Number
        {
            return this._maxScrollPosition;
        }// end function

        public function set maxScrollPosition(param1:Number) : void
        {
            this.setScrollProperties(this._pageSize, this._minScrollPosition, param1);
            return;
        }// end function

        public function get pageSize() : Number
        {
            return this._pageSize;
        }// end function

        public function set pageSize(param1:Number) : void
        {
            if (param1 > 0)
            {
                this._pageSize = param1;
            }
            return;
        }// end function

        public function get pageScrollSize() : Number
        {
            return this._pageScrollSize == 0 ? (this._pageSize) : (this._pageScrollSize);
        }// end function

        public function set pageScrollSize(param1:Number) : void
        {
            if (param1 >= 0)
            {
                this._pageScrollSize = param1;
            }
            return;
        }// end function

        public function get lineScrollSize() : Number
        {
            return this._lineScrollSize;
        }// end function

        public function set lineScrollSize(param1:Number) : void
        {
            if (param1 > 0)
            {
                this._lineScrollSize = param1;
            }
            return;
        }// end function

        public function get orientation() : String
        {
            return this._orientation;
        }// end function

        public function set orientation(param1:String) : void
        {
            if (this._orientation == param1)
            {
                return;
            }
            this._orientation = param1;
            var _loc_2:* = this._orientation == BUIConstants.HORIZONTAL;
            if (_loc_2 && rotation == 0)
            {
                rotation = -90;
                scaleX = -1;
            }
            else if (!_loc_2 && rotation == -90)
            {
                rotation = 0;
                scaleX = 1;
            }
            invalidate(Invalidation.SIZE);
            return;
        }// end function

        override protected function initUI() : void
        {
            super.initUI();
            this.track = new Button();
            this.track.setPosition(0, WIDTH);
            this.track.useHandCursor = false;
            this.track.autoRepeat = true;
            addChild(this.track);
            this.thumb = new LabelButton();
            this.thumb.label = "";
            this.thumb.setSize(WIDTH, 15);
            this.thumb.setPosition(0, 15);
            addChild(this.thumb);
            this.down = new Button();
            this.down.setSize(WIDTH, WIDTH);
            this.down.autoRepeat = true;
            addChild(this.down);
            this.up = new Button();
            this.up.setSize(WIDTH, WIDTH);
            this.up.setPosition(0, 0);
            this.up.autoRepeat = true;
            addChild(this.up);
            this.up.addEventListener(BUIEvent.BUTTON_REPEAT, this.handleScrollPress, false, 0, true);
            this.down.addEventListener(BUIEvent.BUTTON_REPEAT, this.handleScrollPress, false, 0, true);
            this.track.addEventListener(BUIEvent.BUTTON_REPEAT, this.handleScrollPress, false, 0, true);
            this.thumb.addEventListener(MouseEvent.MOUSE_DOWN, this.handleThumbPress, false, 0, true);
            this.enabled = false;
            return;
        }// end function

        override protected function draw() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:* = undefined;
            if (isInvalid(Invalidation.SIZE))
            {
                _loc_1 = _height;
                this.down.setPosition(0, Math.max(this.up.height, _loc_1 - this.down.height));
                this.track.setSize(WIDTH, Math.max(0, _loc_1 - (this.down.height + this.up.height)));
                this.updateThumb();
            }
            if (isInvalid(Invalidation.STYLES))
            {
                _loc_2 = getSkinInstance(getStyleValue("skin"));
                this.track.setStyle("skin", _loc_2["track"]);
                this.thumb.setStyle("skin", _loc_2["thumb"]);
                this.thumb.setStyle("iconSkin", _loc_2["thumbIcon"]);
                this.down.setStyle("skin", _loc_2["down"]);
                this.up.setStyle("skin", _loc_2["up"]);
            }
            this.down.drawNow();
            this.up.drawNow();
            this.track.drawNow();
            this.thumb.drawNow();
            validate();
            return;
        }// end function

        protected function handleScrollPress(event:BUIEvent) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            event.stopImmediatePropagation();
            if (event.currentTarget == this.up)
            {
                this.setScrollPosition(this._scrollPosition - this._lineScrollSize);
            }
            else if (event.currentTarget == this.down)
            {
                this.setScrollPosition(this._scrollPosition + this._lineScrollSize);
            }
            else
            {
                _loc_2 = this.track.mouseY / this.track.height * (this._maxScrollPosition - this._minScrollPosition) + this._minScrollPosition;
                _loc_3 = this.pageScrollSize == 0 ? (this.pageSize) : (this.pageScrollSize);
                if (this._scrollPosition < _loc_2)
                {
                    this.setScrollPosition(Math.min(_loc_2, this._scrollPosition + _loc_3));
                }
                else if (this._scrollPosition > _loc_2)
                {
                    this.setScrollPosition(Math.max(_loc_2, this._scrollPosition - _loc_3));
                }
            }
            return;
        }// end function

        protected function handleThumbPress(event:MouseEvent) : void
        {
            this.isDragging = true;
            this.thumbScrollOffset = mouseY - this.thumb.y;
            this.thumb.mouseStateLocked = true;
            mouseChildren = false;
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.handleThumbDrag, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.handleThumbRelease, false, 0, true);
            return;
        }// end function

        protected function handleThumbDrag(event:MouseEvent) : void
        {
            var _loc_2:* = Math.max(0, Math.min(this.track.height - this.thumb.height, mouseY - this.track.y - this.thumbScrollOffset));
            this.setScrollPosition(_loc_2 / (this.track.height - this.thumb.height) * (this._maxScrollPosition - this._minScrollPosition) + this._minScrollPosition);
            event.updateAfterEvent();
            return;
        }// end function

        protected function handleThumbRelease(event:MouseEvent) : void
        {
            this.isDragging = false;
            mouseChildren = true;
            this.thumb.mouseStateLocked = false;
            if (!stage)
            {
                return;
            }
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.handleThumbDrag);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.handleThumbRelease);
            return;
        }// end function

        public function setScrollPosition(param1:Number, param2:Boolean = true) : void
        {
            var _loc_3:* = this.scrollPosition;
            this._scrollPosition = Math.max(this._minScrollPosition, Math.min(this._maxScrollPosition, param1));
            if (_loc_3 == this._scrollPosition)
            {
                return;
            }
            if (param2)
            {
                dispatchEvent(new ScrollEvent(this._orientation, this.scrollPosition - _loc_3, this.scrollPosition));
            }
            this.updateThumb();
            return;
        }// end function

        protected function updateThumb() : void
        {
            var _loc_1:* = this._maxScrollPosition - this._minScrollPosition + this._pageSize;
            if (this.track.height <= THUMB_MIN_HEIGHT || this._maxScrollPosition <= this._minScrollPosition || (_loc_1 == 0 || isNaN(_loc_1)))
            {
                this.thumb.height = THUMB_MIN_HEIGHT;
                this.thumb.visible = false;
            }
            else
            {
                this.thumb.height = Math.max((THUMB_MIN_HEIGHT + 1), this._pageSize / _loc_1 * this.track.height);
                this.thumb.y = this.track.y + (this.track.height - this.thumb.height) * ((this._scrollPosition - this._minScrollPosition) / (this._maxScrollPosition - this._minScrollPosition));
                this.thumb.visible = enabled;
            }
            return;
        }// end function

    }
}
